package com.personnel.service.impl;

import com.personnel.pojo.LaborContract;
import com.personnel.mapper.LaborContractMapper;
import com.personnel.service.LaborContractService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * (LaborContract)表服务实现类
 *
 * @author yuepeng
 * @version 1.0
 * @since 2022-03-30 15:44:51
 */
@Service("laborContractService")
public class LaborContractServiceImpl implements LaborContractService {
    @Resource
    private LaborContractMapper laborContractMapper;

    /**
     * 根据模糊条件查询总个数
     *
     * @return 返回查询到的总个数
     */
    public Map<String, Object> selectForCount(String staffName, String contractType, String contractState, String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", this.laborContractMapper.selectForCount(staffName, contractType, contractState, startDate, endDate));
        return map;
    }

    /**
     * 查询所有数据
     *
     * @return 返回所有数据
     */
    public Map<String, Object> selectAll() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", this.laborContractMapper.selectAll());
        return map;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param laborContractId 主键
     * @return 实例对象
     */
    public Map<String, Object> selectById(Integer laborContractId) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", this.laborContractMapper.selectById(laborContractId));
        return map;
    }

    /**
     * 查询分页数据
     *
     * @param index 查询起始位置
     * @return 对象列表
     */
    public Map<String, Object> selectForPage(int index, int limit, String staffName, String contractType, String contractState,String startDate,String endDate) {
        // 获取当前表中的总记录
        int tableCount = this.laborContractMapper.selectForCount(staffName, contractType, contractState, startDate, endDate);
        // 总页码计算   (总条数 - 1) / 每页显示条数  + 1
        // (100 - 1) / 10 + 1 = 10        (101 - 1) / 10 + 1 = 11      (99 - 1) / 10 + 1 = 10
        int pageCount = (tableCount - 1) / 10 + 1;
        // 计算每页开始的下标值
        index = (index - 1) * 10;
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("pageCount", pageCount);  // 查询的记录总页码
        map.put("count", tableCount);     // 当前表中的总条数
        map.put("data", this.laborContractMapper.selectForPage(index, limit, staffName, contractType, contractState,startDate,endDate));
        return map;
    }

    /**
     * 新增数据
     *
     * @param laborContract 实例对象
     * @return 实例对象
     */
    public Map<String, Object> insert(LaborContract laborContract) {
        this.laborContractMapper.insert(laborContract);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "新增成功");
        return map;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param laborContract 实例对象
     * @return 实例对象
     */
    public Map<String, Object> updateById(LaborContract laborContract) {
        this.laborContractMapper.updateById(laborContract);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "更新成功");
        return map;
    }

    /**
     * 通过主键删除数据
     *
     * @param ids 主键
     * @return 是否成功
     */
    public Map<String, Object> deleteByIds(String ids) {
        String[] idArray = ids.split(",");
        List<String> list = new ArrayList<>();
        list.addAll(Arrays.asList(idArray));
        this.laborContractMapper.deleteByIds(list);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "删除成功");
        return map;
    }
}